<?php 
if(!defined('BASEPATH'))
		exit('No direct script access allowed');
	class Doctors_Category extends CI_Controller {
		function __construct(){
			session_start();
			parent::__construct();
			$this->load->library('session');
		}
		
		function index($query_id=0, $order_by  = 'created_date', $sort_order = 'desc', $current_page = 0){
			$search_by='';
			$search_by_value='';
				if($query_id == '0'){		
					//echo '<pre>';print_r($_SESSION);exit;
					unset($_SESSION['search']);
				}else{
					if(isset($_SESSION['search'])){
					$array = $_SESSION['search'];
					//echo '<pre>';print_r($_SESSION);exit;
					$search_by = $array['search_by'];
					$search_by_value = $array['search'];
					}
				}			
			$results_per_page = 2;
		
		
			$url = CURL_URL;
			$data = array('www-command'=>'doctors-all_category', 'current_page'=>$current_page, 'results_per_page'=>$results_per_page,'results_per_page'=>$results_per_page,'sort_order'=>$sort_order,'order_by'=>$order_by,'search_by'=>$search_by,'search_by_value'=>$search_by_value);
			
			$string = http_build_query($data);
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$response= json_decode(curl_exec($ch));
			if($response->message !== 'No Doctors Categories'){
			$data['doctors_all_category'] = $response;
			
			//echo '<pre>';print_r($response);exit;
			curl_close($ch);
		
			$this->load->library('pagination');			
			$config['base_url'] = base_url().'admin/doctors_category/index/'.$query_id.'/'.$order_by.'/'.$sort_order;
			$config['per_page'] = $results_per_page;
			$config['total_rows']  = $response->total;
			$config['uri_segment'] = 7;
			$config['num_links'] = 1;
			$config['prev_link'] = 'Previous';
			$config['next_link'] = 'Next';
			$config['first_link'] = 'First';
			$config['last_link'] = 'Last';
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			$data['error'] = "";
			}else{
				$data['error'] = 1;
				$data['doctors_all_category'] = "";
			}
			//echo '<pre>';print_r($data);exit;
			$data['doctors_category_fileds'] = array('category_name'=>'Category Name', 'admin_id'=>"Admin's Id",'created_date'=>'Date');
			$data['order_by'] = $order_by;
			$data['sort_order'] = $sort_order;
			$data['query_id'] = $query_id;
			$data['search_by'] = $search_by;
			$data['search_by_value'] = $search_by_value;
			$this->load->view('admin/doctors_category', $data);
 		}
		
		function search_by(){
			if($this->input->post('search') == ''){
				$search = $this->input->post('search_date');
			}else{
				$search = $this->input->post('search');	
			}
				
			$query_array = array(
				'search_by'=>$this->input->post('search_by'),
				'search'=>$search
			);
			$query_id=rand();
			$_SESSION['search']=$query_array;
			//echo '<pre>';print_r($_SESSION['search']);exit;
			redirect('admin/doctors_category/index/'.$query_id);
			
		}	
		
		/* function create_doctors_category_page(){
			$this->load->view('admin/create_doctors_category_page');
		} */
		
		function create_doctors_category(){
				$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('name', 'Category Name', 'required|alpha');
				
				if($this->form_validation->run() == FALSE){
						$this->load->view('admin/create_doctors_category_page');
				}else{
					$url = 'http://localhost/azindia/azindia-api/json.api';
					$data = array('www-command'=>'doctors-create_category', 
					'category_name' => $this->input->post('name'),
					'admin_id' => $this->session->userdata('admin_id')
					);
					//echo '<pre>';print_r($data);exit;
					$string = http_build_query($data);
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/doctors_category/index/');
				}
			}
			
			/* function edit_doctors_category_page(){
					$url = CURL_URL;
					$data = array('www-command'=>'doctors-get_category', 
					'category_id' => $this->uri->segment(4)
					);
					//echo '<pre>';print_r($data);exit;
					$string = http_build_query($data);
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//$data['organizations_get_organization'] = $response;
					//echo '<pre>';print_r($response);exit;
					$response = (array) $response;
					foreach($response as $value){
						$value = $value;
					}
					$data['name'] = $value[0]->category_name;
					curl_close($ch);
				$this->load->view('admin/edit_doctors_category_page', $data);
			} */
			
			function edit_doctors_category(){
				$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('name', 'Category Name', 'required|alpha');
				
				if($this->form_validation->run() == FALSE){
						$url = CURL_URL;
						if($this->uri->segment(4) > 0){
							$_SESSION['categoryid'] = $this->uri->segment(4);
						}
					$data = array('www-command'=>'doctors-get_category', 
					'category_id' => $_SESSION['categoryid']
					);
					//echo '<pre>';print_r($data);exit;
					$string = http_build_query($data);
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//$data['organizations_get_organization'] = $response;
					//echo '<pre>';print_r($response);exit;
					$response = (array) $response;
					foreach($response as $value){
						$value = $value;
					}
					$data['name'] = $value[0]->category_name;
					curl_close($ch);
				$this->load->view('admin/edit_doctors_category_page', $data);
				}else{
					$url = 'http://localhost/azindia/azindia-api/json.api';
					$data = array('www-command'=>'doctors-edit_category', 
					'category_name' => $this->input->post('name'),
					'category_id' => $_SESSION['categoryid']
					);
				//echo '<pre>';print_r($data);exit;
					$string = http_build_query($data);
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/doctors_category/index/');
				}
			}
			
			function delete_doctors_category(){
				$url = 'http://localhost/azindia/azindia-api/json.api';
				if($this->uri->segment(5) == 0){
					$status = 1;
				}else{
					$status = 0;
				}
				$data = array('www-command'=>'doctors-delete_category','status'=>$status, 'category_id'=>$this->uri->segment(4));
				$string = http_build_query($data);
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				
				redirect('admin/doctors_category/index/');
			}
	}
